package com.rgs.base.core.util.excel;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.rgs.base.core.system.AsyncTaskStatusEnum;
import com.rgs.base.core.util.excel.bo.ImportFileErrorBO;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ren Gaoshuai
 * @date 2022-03-08 15:45
 **/
public abstract class AbstractAnalysisExcelListener<D, T> extends AnalysisEventListener<T> {
    private static final int BATCH_COUNT = 50;

    //
    private List<T> tList = new ArrayList<>();
    ExcelWriter excelWriter;
    WriteSheet writeSheet;


    private AsyncTaskStatusEnum asyncTaskStatus;
    private long errCount;
    private long successCount;

    public AbstractAnalysisExcelListener(File errFileTemp, String titleName) {
        writeSheet = ExcelUtil.initWriteSheet(ImportFileErrorBO.class, titleName);
        excelWriter = ExcelUtil.initExcelWriter(ImportFileErrorBO.class, errFileTemp);
    }

    protected abstract List<ImportFileErrorBO> saveData(List<D> tList);

    protected abstract List<D> mapper(List<T> tList);

    public long getErrCount() {
        return this.errCount;
    }

    public long getSuccessCount() {
        return this.successCount;
    }

    public AsyncTaskStatusEnum getAsyncTaskStatus() {
        return this.asyncTaskStatus;
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        tList.add(data);
        if (tList.size() >= BATCH_COUNT) {

            List<ImportFileErrorBO> importFileErrorList = saveData(mapper(tList));
            errCount += importFileErrorList.size();
            successCount += tList.size() - errCount;

//            List<Map<String, Object>> collect = importFileErrorList.stream().map(ImportFileErrorBO::getBeanMap)
//                    .collect(Collectors.toList());
            ExcelUtil.writeExcel(writeSheet, excelWriter, importFileErrorList, ImportFileErrorBO.class);
//            --excelWriter.write(collect, writeSheet);
            // 存储完成清理 list
            tList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        List<ImportFileErrorBO> importFileErrorList = saveData(mapper(tList));
        errCount += importFileErrorList.size();
        successCount += tList.size() - importFileErrorList.size();
//        List<Map<String, Object>> collect = importFileErrorList.stream().map(ImportFileErrorBO::getBeanMap)
//                .collect(Collectors.toList());
//        --excelWriter.write(collect, writeSheet);
        ExcelUtil.writeExcel(writeSheet, excelWriter, importFileErrorList, ImportFileErrorBO.class);
        //
        excelWriter.finish();
        if (errCount == 0L) {
            asyncTaskStatus = AsyncTaskStatusEnum.SUCCEED;
        } else {
            asyncTaskStatus = AsyncTaskStatusEnum.PART_SUCCEED;
        }

    }
}

